import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from joblib  import load

class NN(object):
    
    def __init__(self):
        self.loaded_model = tf.keras.models.load_model('5/my_model.keras')
        self.scaler = load('5/scaler.joblib')

    # def create_dataset(self, data, n_steps):
    #     """构建数据集"""
    #     x, y = [], []
    #     for i in range(len(data) - n_steps):
    #         x.append(data[i:i+n_steps])
    #         y.append(data[i+n_steps, :18])  # 注意这里应该是:1来获取最后一列作为标签，原代码有误
    #     return np.array(x), np.array(y)

    def get_hourly_trend(self):
        n_steps = 7  # 长度七天
        df_pivot = pd.read_csv('./5/scenic_data.csv')
        latest_data = df_pivot.iloc[-n_steps:].values #取最后七天数据
        latest_data = latest_data.reshape(1, n_steps,latest_data.shape[1])
        
        predicted = self.loaded_model.predict(latest_data)
        predicted_counts = self.scaler.inverse_transform(predicted) #反归一化
        predicted_counts[predicted_counts < 0] = 0 #修正负数
        
        hourly_trend = predicted_counts[0].astype(int).to_list()
        # print(hourly_trend)
        print(predicted_counts)
        
if __name__ == '__main__':
    n = NN()
    n.get_hourly_trend()
        